﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Web.UI.WebControls;

namespace VIT.Web.Admin
{
    public partial class ModuleList : VITComponent
    {
        public string currentLang = string.Empty;
        Module obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            currentLang = Session["language"].ToString();
            obj = new Module();
            Language lang = new Language();

            if (!Page.IsPostBack)
            {
                Session["order"] = "Orders";
                loadLang(lang);
                load();
            }
        }

        private void load()
        {
            rpt.DataSource = obj.GetAll(currentLang, Session["order"].ToString(), Convert.ToBoolean(Session["desc"]));
            rpt.DataBind();
        }
        private void loadLang(Language lang)
        {
            ddlLang.DataSource = lang.getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = currentLang; 
        }

        public void deleteList(string[] listId)
        {
            foreach (string id in listId)
                obj.Delete(1, int.Parse(id));
        }
        public void PublishList(int pub, string[] listId)
        {
            foreach (string id in listId)
                obj.Publish(pub, int.Parse(id));
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
            load();
        }
        protected void insert_Click(object sender, EventArgs e)
        {
            TransferComponent("Config", "ModuleEdit");
        }
        protected void btPublished_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(1, check);
            load();
        }
        protected void btUnPublished_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(0, check);
            load();
        }
        
        protected void changePublish_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            int pub = (id[1] == "True") ? 0 : 1;
            obj.Publish(pub, int.Parse(id[0]));
            load();
        }

        protected void updateOrder_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            obj.Order(id[1], int.Parse(id[0]));
            load();
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentLang = ddlLang.SelectedValue;
            load();
        }

        protected void sortedChange_OnClick(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            Session["desc"] = !Convert.ToBoolean(Session["desc"]);
            Session["order"] = btn.Param.ToString();
            load();
        }
    }
}
